package es.efor.ics.gev.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Un {@link AtributoMaestro atributo} en una {@link Persona persona} + valor
 * 
 * @author dlisbona
 */
@Entity
@Table(name = "VAL_ATRIBUTOS_PERSONAS")
public class AtributoPersonaInformado extends BaseObject implements
		Serializable {

	/**
	 * Embedded composite identifier class that represents the primary key
	 * columns of the many-to-many join table.
	 */
	@Embeddable
	public static class Id implements Serializable {

		private static final long serialVersionUID = -7491129276464340152L;

		private Long personaId;
		private Long atributoId;

		public Id() {
		}

		public Id(Long personaId, Long atributoId) {
			this.personaId = personaId;
			this.atributoId = atributoId;
		}

		@Column(name = "PERSONA_ID")
		public Long getPersonaId() {
			return personaId;
		}

		public void setPersonaId(Long personaId) {
			this.personaId = personaId;
		}

		@Column(name = "ATRIBUTO_ID")
		public Long getAtributoId() {
			return atributoId;
		}

		public void setAtributoId(Long atributoId) {
			this.atributoId = atributoId;
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof Id) {
				Id that = (Id) o;
				return this.personaId.equals(that.personaId)
						&& this.atributoId.equals(that.atributoId);
			} else {
				return false;
			}
		}

		@Override
		public int hashCode() {
			return personaId.hashCode() + atributoId.hashCode();
		}

		@Override
		public String toString() {
			return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
					.append("personaId", this.personaId).append("atributoId",
							this.atributoId).toString();
		}

	}

	// static data
	private static final long serialVersionUID = -7491129276464340152L;

	// private instance data
	private Id id;
	private Persona persona;
	private AtributoMaestro atributo;
	private String valor;
	private Integer version;

	/**
	 * Construct an uninitialized <code>AtributoPersonaInformado</code> (for runtime
	 * proxy generation in Hibernate).
	 */
	protected AtributoPersonaInformado() {		
	}

	/**
	 * Construct an initialized <code>AtributoPersonaInformado</code>.
	 * 
	 * @param persona
	 * @param atributo
	 * @param valor
	 */
	public AtributoPersonaInformado(Persona persona, AtributoMaestro atributo,
			String valor) {
		this.persona = persona;
		this.atributo = atributo;
		this.valor = valor;
		
		// Set primary key
		this.id = new Id(persona.getId(), atributo.getId());
	}

	// ********************** Accessor Methods ********************** //

	@EmbeddedId
	public Id getId() {
		return id;
	}

	public void setId(Id id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PERSONA_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_VAL_ATR_PERS_PERSONA_ID")
	public Persona getPersona() {
		return persona;
	}

	public void setPersona(Persona persona) {
		this.persona = persona;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ATRIBUTO_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_VAL_ATR_PERS_ATRIBUTO_ID")
	public AtributoMaestro getAtributo() {
		return atributo;
	}

	public void setAtributo(AtributoMaestro atributo) {
		this.atributo = atributo;
	}

	@Column(name = "VALOR_ATRIBUTO")
	public String getValor() {
		return valor;
	}

	public void setValor(String valor) {
		this.valor = valor;
	}

	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	// ********************** Common Methods ********************** //

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		final AtributoPersonaInformado that = (AtributoPersonaInformado) o;
		return id.equals(that.id);
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"persona", this.getPersona()).append("atributo",
				this.getAtributo()).append("valor", this.getValor()).toString();
	}

}
